package token

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"regexp"
	"strings"
)

// TODO 这样写合适吗？
var EOF EOFToken = EOFToken{LineNumber: -1}

type Lexer struct {
	tokens []Token
}

func (lexer *Lexer) Read() Token {
	return &EOFToken{
		LineNumber: -1,
	}
}

func (lexer *Lexer) Peek(i int) Token {
	length := len(lexer.tokens)
	if i > length {
		return &EOF
	}
	token := lexer.tokens[i]
	return token
}

func ReadFile(filePath string) {
	file, err := os.Open(filePath)
	if err != nil {
		panic(err)
	}

	reader := bufio.NewReader(file)
	for {
		lineBytes, err := reader.ReadBytes('\n')
		if err != nil && err != io.EOF {
			panic(err)
		}
		if err == io.EOF {
			break
		}
		line := strings.TrimSpace(string(lineBytes))
		fmt.Println(line)
	}
}

const RegexPat string = "\\s*((//.*)|([0-9]+)|(\"(\\\\\"|\\\\\\\\|\\\n|[^\"])*\")[A-Z_a-z][A-Z-z0-9]*|==|<=|>=|&&|\\|\\||\\p{Punct})?"

func Parse(line string) {
	//TODO 使用正则找到所有的token
	re := regexp.MustCompile(RegexPat)

	finded := re.FindStringSubmatch(line)

	for _, ele := range finded {
		fmt.Println(ele)
	}
}
